app.controller('GoodsSkuCtrl', ['$scope', '$state','$stateParams','$document','GoodsSkuService', 'toaster', function ($scope, $state,$stateParams,$document, GoodsSkuService, toaster) {
    $document.on("click", function( event ){
            hideAll();
        }
    );

    var hideAll=function(){
        for(var i=0;i<$scope.skus.length;i++){
            $scope.$apply(function(){
                $scope.skus[i].selectShow=false;
            })
            for(var j=0;j<$scope.skus[i].skuItems.length;j++){
                $scope.$apply(function(){
                    $scope.skus[i].skuItems[j].selectShow=false;
                })
            }
        }
    }

    //获得skuall
    $scope.getSkuAll = function () {
        GoodsSkuService.getSkuAll($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shopSkus = result.data;
                    break;
                default :
                    break;
            }
        });
    }

    $scope.getSkuAll();

    //选择sku
    $scope.selectSku = function (item, sku) {
        item.id = sku.id;
        item.name = sku.name;
        item.selectShow=false;

        var useState=skuHasUse(item.name);
        if(useState>=2){
            alert("你已经选择过该规格名称");
            item.name='';
            initSku(item);
            return;
        }

        initSku(item);
    }


    //sku
    $scope.index = 0;

    //增加规格项目
    $scope.addSkuContainer = function () {
        if ($scope.index >= 3) {
            return;
        }

        $scope.index++;

        var sku = new Object();
        sku.name = '';
        sku.skuItems = new Array();
        $scope.skus.push(sku);
    }


    $scope.allowToAddSku = true;

    //添加sku
    $scope.addSku = function (item) {
        if (!$scope.allowToAddSku) {
            return;
        }
        $scope.allowToAddSku = false;
        GoodsSkuService.sku.name = item.name;
        GoodsSkuService.addSku($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    item.id = result.data.id;
                    $scope.selectSku(item,item);
                    initSkuItems(item);
                    $scope.allowToAddSku = true;
                    GoodsSkuService.sku = {};
                    break;
                case 618:
                    $scope.allowToAddSku = true;
                    break;
                default :
                    break;
            }
        });
    }

    //判断sku是否已经存在
    var forItem = function (item) {
        var state = false;
        if (angular.isUndefined($scope.shopSkus)) {
            return state;
        }

        for (var i = 0; i < $scope.shopSkus.length; i++) {
            if (!angular.isUndefined($scope.shopSkus[i].name)) {
                if (item.name == $scope.shopSkus[i].name) {
                    item.id = $scope.shopSkus[i].id;
                    $scope.selectSku(item,$scope.shopSkus[i]);
                    initSkuItems(item);
                    return true;
                }
            }
        }

        return state;
    }

    //判断一下sku是否正在用
    var skuHasUse=function(name){
        var index=0;
        for(var i=0;i<$scope.skus.length;i++){
            if(name===$scope.skus[i].name){
                index++;
            }
        }

        return index;
    }


    //sku input focus
    $scope.skuFocus = function (item) {
        item.selectShow = true;
    }

    //sku input blur
    $scope.skuBlur = function (item) {
        var useState=skuHasUse(item.name);
        if(useState>=2){
            alert("请不要填写重复规格名称");
            item.name='';
            initSku(item);
            return;
        }



        var state = forItem(item);

        if (state) {
            $scope.getSkuAll();
            return;
        }

        if (angular.isUndefined(item.name) || item.name == '') {
            return;
        }

        $scope.addSku(item);
    }

    //初始化SKU
    var initSku = function (item) {
        item.skuItems.splice(0, item.skuItems.length);
        var skuItem = new Object();
        skuItem.chainSkuId = item.id;
        item.skuItems.push(skuItem);
    }

    //初始化skuItems
    var initSkuItems = function (item) {
        if (item.skuItems.length > 0) {
            for (var i = 0; i < item.skuItems.length; i++) {
                item.skuItems[i].chainSkuId = item.id;
                item.skuItems[i].skuItemsSort=i;
            }
            return;
        }
        var skuItem = new Object();
        skuItem.chainSkuId = item.id;
        item.skuItems.push(skuItem);
    }

    //添加sku盒子
    $scope.addSkuItemsContainer = function (item) {
        if (angular.isUndefined(item.id)) {
            toaster.pop('waiting', "提示", "请先添加规格名");
            return;
        }
        var skuItem = new Object();
        skuItem.chainSkuId = item.id;
        item.skuItems.push(skuItem);

    }

    $scope.delSku = function (item) {
        var index = $scope.skus.indexOf(item);
        if (index > -1) {
            $scope.skus.splice(index, 1);
            $scope.index--;
        }
        if($scope.skus.length==0){
            $scope.addSkuContainer();
            alert("开启规格设置至少需要填写一个规格");
        }
        $scope.initSkuSort();
        roundInfo();
    }

    $scope.initSkuSort=function(){
        for(var i=0;i<$scope.skus.length;i++){
            for(var j=0;j<$scope.skus[i].skuItems.length;j++){
                $scope.skus[i].skuItems[j].sort=i;
            }
        }
    }

    //sku_items
    $scope.getSkuItemsAll = function (subItem, item) {
        GoodsSkuService.getSkuItemsAll($scope, item.id).then(function (result) {
            switch (result.status) {
                case 200:
                    subItem.selectSkuItems = result.data;
                    skuItemsHasUse(subItem, item);
                    break;
                default :
                    break;
            }
        });
    }

    $scope.delSkuItems = function (subItem, item) {
        var index = item.skuItems.indexOf(subItem);
        if (index > -1) {
            item.skuItems.splice(index, 1);
        }
        initSkuItems(item);
        roundInfo();
    }

    $scope.selectSkuItems=function(subItem,skuItems){
        subItem.standard=skuItems.standard;
        subItem.id=skuItems.id;
        roundInfo();
    }

    $scope.skuItemsFocus = function (subItem, item) {
        subItem.selectShow = true;
        $scope.getSkuItemsAll(subItem, item);
    }

    $scope.skuItemsBlur = function (subItem, item) {
        if (angular.isUndefined(subItem.standard) || subItem.standard === '') {
            return;
        }

        GoodsSkuService.skuItems = subItem;

        var state = forSkuItems(subItem);
        if (state) {
            return;
        }

        removeUseSkuItems(subItem, item);

        $scope.addSkuItems(subItem, item);

    }

    $scope.addSkuItems = function (subItem, item) {
        GoodsSkuService.addSkuItems($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    subItem.id = result.data.id;
                    $scope.selectSkuItems(subItem,GoodsSkuService.skuItems);
                    GoodsSkuService.skuItems = {};
                    roundInfo();
                    break;
                case 630:
                    $scope.getSkuItemsAll(subItem, item);
                    break;
                default :
                    break;
            }
        });
    }


    $scope.initSort=function(subItem,parentIndex,index){
        subItem.sort=parentIndex;
        subItem.skuItemsSort=index;
    }



    var forSkuItems = function (subItem) {
        var state = false;
        if (angular.isUndefined(subItem.selectSkuItems)) {
            return;
        }

        for (var i = 0; i < subItem.selectSkuItems.length; i++) {
            if (!angular.isUndefined(subItem.selectSkuItems[i].name)) {
                if (subItem.standard == subItem.selectSkuItems[i].standard) {
                    subItem.id = subItem.selectSkuItems.id;
                    state = true;
                }
            }
        }
        return state;
    }

    //移除重复的数据
    var removeUseSkuItems=function(subItem,item){
        var index=item.skuItems.indexOf(subItem);
        for (var i = 0; i < item.skuItems.length; i++) {
            if(item.skuItems[i].standard==subItem.standard && i!=index){
                alert("请勿输入重复的规格值");
                $scope.delSkuItems(subItem,item);
            }
        }
    }

    var skuItemsHasUse = function (subItem, item) {
        var arr = new Array();
        for (var i = 0; i < subItem.selectSkuItems.length; i++) {
            if(subItem.selectSkuItems[i].standard==subItem.standard){
                $scope.selectSkuItems(subItem,subItem.selectSkuItems[i]);
            }
            arr.push(subItem.selectSkuItems[i].standard);
        }

        for (var i = 0; i < item.skuItems.length; i++) {
            var index = arr.indexOf(item.skuItems[i].standard);
            if (index > -1) {
                arr.splice(index, 1);
                subItem.selectSkuItems.splice(index, 1);
            }
        }

    }

    //skucontents 规格明细
    $scope.unifiedPrice=0.00;
    $scope.unifiedStock=0;

    var roundInfo = function () {
        $scope.skuContents = new Array();
        round($scope.skus, $scope.skus[0], '', '',[]);
    }

    var round = function (list, arr, str, key,temp) {
        for (var i = 0; i < list.length; i++) {
            if (i == list.indexOf(arr)) {
                for (var j = 0; j < arr.skuItems.length; j++) {
                    if(!angular.isUndefined(arr.skuItems[j].standard)){
                        var st = arr.skuItems[j].standard;
                        var ke = arr.skuItems[j].id;
                        var tempArr=[];
                        tempArr.push(st);
                        if (str != '' && key != '') {
                            st = str + '+' + st;
                            ke = key + '_' + ke;
                            tempArr.unshift.apply(tempArr,temp);
                        }
                        if (i < list.length - 1) {
                            round(list, list[i + 1], st, ke,tempArr);
                        } else if (i == list.length - 1) {
                            var skuContent = new Object();
                            skuContent.standards = st;
                            skuContent.key = ke;
                            skuContent.status = 1;
                            //if (!angular.isUndefined(st)) {
                            //    skuContent.goodsSkuItems = st.split('+');
                            //}
                            skuContent.goodsSkuItems = tempArr;
                            $scope.skuContents.push(skuContent);
                        }
                    }
                }
            }
        }

        $scope.$emit('skuContents', $scope.skuContents);
    }

    //统一设置价格库存
    $scope.saveSkuPrice=function(unifiedPrice){
        for(var i=0;i<$scope.skuContents.length;i++){
            $scope.skuContents[i].price=unifiedPrice;
        }
    }

    $scope.saveSkuStock=function(unifiedStock){
        for(var i=0;i<$scope.skuContents.length;i++){
            $scope.skuContents[i].stock=unifiedStock;
        }
    }



    //获取商品相关SKU名称和规格
    $scope.goodsId=$stateParams.id;

    $scope.queryGoodsSkuItems=function(){

        if(angular.isUndefined($scope.goodsId)){
            return;
        }

        GoodsSkuService.queryGoodsSkuItems($scope).then(function(result){
            switch (result.status) {
                case 200:
                    var skus=new Array();
                    for(var i=0;i<result.data.length;i++){
                        var sku=new Object();
                        sku.id=result.data[i].chainSkuId;
                        sku.name=result.data[i].name;

                        var skuItems=new Array();
                        for(var j=0;j<result.data[i].childrens.length;j++){
                            var skuItem=new Object();
                            skuItem.id=result.data[i].childrens[j].chainSkuItemsId;
                            skuItem.chainSkuId=result.data[i].chainSkuId;
                            skuItem.standard=result.data[i].childrens[j].standard;
                            skuItems.push(skuItem);
                        }
                        sku.skuItems=skuItems;
                        skus.push(sku);
                    }
                    $scope.skus=skus;
                    $scope.$emit('skus',$scope.skus);
                    roundInfo();

                    $scope.querySkuContent();

                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.queryGoodsSkuItems();

    //查询skucontent
    $scope.querySkuContent=function(){
        if(angular.isUndefined($scope.goodsId)){
            return;
        }

        GoodsSkuService.querySkuContent($scope).then(function(result){
            switch (result.status) {
                case 200:
                    for(var i=0;i<result.data.length;i++){
                        var key=result.data[i].key;
                        for(var j=0;j<$scope.skuContents.length;j++){
                            if(key===$scope.skuContents[j].key){
                                $scope.skuContents[j].price=result.data[i].price;
                                $scope.skuContents[j].stock=result.data[i].stock;
                                $scope.skuContents[j].status=result.data[i].status;
                            }
                        }

                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }




}])